package com.open.capacity.study.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.StageIteration;
import com.open.capacity.common.model.SysTeaching;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.study.dao.StageIterationDao;
import com.open.capacity.study.service.StageIterationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StageIterationServiceImpl implements StageIterationService {
    @Autowired
    private StageIterationDao stageIterationDao;

    /**
     * 新增迭代阶段
     * @param stageIter
     * @return
     * @throws ServiceException
     */
    @Override
    public Result insert(StageIteration stageIter) throws ServiceException {
        try {
            int res = stageIterationDao.insert(stageIter);
            stageIter.setCretim(new Date());
            log.info("新增学习阶段迭代：{}",stageIter);
            return res > 0 ? Result.succeed(stageIter,"新增成功"):Result.failed("新增失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    /**
     * 修改可用状态
     * @param params
     * @return
     * @throws ServiceException
     */
    @Override
    public Result updateStatus(Map<String, Object> params) throws ServiceException {
        try {
            Integer id = MapUtils.getInteger(params,"stitid");
            int status = MapUtils.getIntValue(params,"status");
            StageIteration si = stageIterationDao.findById(id);
            if (si == null){
                return Result.failed("不存在该迭代阶段");
            }
            si.setStatus(status);
            si.setUpdtim(new Date());
            int res = stageIterationDao.updateStatusById(si);
            log.info("修改迭代阶段状态：{}",si);
            return res>0 ? Result.succeed(si,"修改成功") : Result.failed("修改失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional
    public Result update(StageIteration stageIteration) throws ServiceException {
        try {
            int res = stageIterationDao.updateById(stageIteration);
            stageIteration.setUpdtim(new Date());
            log.info("修改迭代阶段：{}",stageIteration);
            return res>0 ? Result.succeed(stageIteration,"修改成功") : Result.failed("修改失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult<StageIteration> findStageIter(Map<String, Object> params) throws ServiceException {
        try {
            int total = stageIterationDao.count(params);
            List<StageIteration> staIterist = Collections.emptyList();
            if (total > 0){
                PageUtil.pageParamConver(params, false);
                staIterist = stageIterationDao.findList(params);
            }
            //收集所有数据
            return PageResult.<StageIteration>builder().data(staIterist).code(0).count((long) total).build();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }
}
